package com.saicmotor.floatingwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private AlertDialog dialog;
    private Button btn_min;
    private EditText edt_des;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_min = findViewById(R.id.btn_min);
        edt_des = findViewById(R.id.edt_des);

        // 服务运行
        // 请求权限
        // 检查版本
        if (isServiceRunning()) {
            stopService(new Intent(this, FloatingWindow.class));
        }

        edt_des.setText(Common.currDes);
        edt_des.setSelection(edt_des.getText().toString().length());
        edt_des.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Common.currDes = edt_des.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        btn_min.setOnClickListener((v) -> {
            if (checkPermission()) {
                startService(new Intent(this, FloatingWindow.class));
                finish();
            } else {
                requestFloatingWindowPermission();
            }
        });
    }


    // 检查服务是否运行
    private boolean isServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo running : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (FloatingWindow.class.getName().equals(running.service.getClassName())) {
                return true;
            }
        }
        return false;
    }


    // 请求权限
    private void requestFloatingWindowPermission() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle("需要悬浮权限！");
        builder.setMessage("设置运行应用悬浮！");
        builder.setPositiveButton("设置", (dialog, which) -> {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, RESULT_OK);
        });
        dialog = builder.create();
        dialog.show();
    }

    // 检查权限
    private boolean checkPermission() {
        boolean res = true;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            res = Settings.canDrawOverlays(this);
        }
        return res;
    }


}